/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-07-24 09:22:17
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-07-29 10:15:42
 * @FilePath: /vue3-uniapp/src/service/useStorage.ts
 */
import {
    ACCESS_TOKEN,
    USER_INFO,
    CURRENT_GRID,
    CURRENT_DEPT,
    THEME_MODE,
    THEME_PRIMARY
} from '@/constants/storage';

import type {
    MyInfoDetail,
    RoleGroupVo,
    UserAreaVo,
    DepartmentVo
} from '@/typings/api/my';

import { useNotifySubject } from './app/useNotifySubject';

type CurrentGridData = {
    roleId: number;
    areaCode: string;
    areaLevel: number;
} & UserAreaVo;

type CurrentDeptData = {
    roleId: number;
    departmentId: number;
} & DepartmentVo;

export const useStorage = () => {
    const { danger } = useNotifySubject();
    const getAccessTokenSync = (): string | undefined => {
        return uni.getStorageSync(ACCESS_TOKEN);
    };

    const setAccessToken = (token: string) => {
        return uni.setStorage({
            key: ACCESS_TOKEN,
            data: token,
            fail: () => {
                console.error(`[./service/useStorage]保存${ACCESS_TOKEN}失败`);
                danger({
                    msg: `[./service/useStorage]保存${ACCESS_TOKEN}失败`
                });
            }
        });
    };

    const removeAccessToken = () => {
        return uni.removeStorageSync(ACCESS_TOKEN);
    };

    const getUserInfoSync = (): MyInfoDetail | undefined => {
        return uni.getStorageSync(USER_INFO);
    };

    const setUserInfo = (data: MyInfoDetail) => {
        return uni.setStorage({
            key: USER_INFO,
            data,
            fail: () => {
                console.error(`[./service/useStorage]保存${USER_INFO}失败`);
                danger({
                    msg: `[./service/useStorage]保存${USER_INFO}失败`
                });
            }
        });
    };

    const removeUserInfo = () => {
        return uni.removeStorageSync(USER_INFO);
    };

    const getCurrentGridSync = (): CurrentGridData | undefined => {
        return uni.getStorageSync(CURRENT_GRID);
    };

    const setCurrentGrid = (data: CurrentGridData) => {
        return uni.setStorage({
            key: CURRENT_GRID,
            data,
            fail: () => {
                console.error(`[./service/useStorage]保存${CURRENT_GRID}失败`);
                danger({
                    msg: `[./service/useStorage]保存${CURRENT_GRID}失败`
                });
            }
        });
    };

    const getCurrentDeptSync = (): CurrentDeptData | undefined => {
        return uni.getStorageSync(CURRENT_DEPT);
    };

    const setCurrentDept = (data: CurrentDeptData) => {
        return uni.setStorage({
            key: CURRENT_DEPT,
            data,
            fail: () => {
                console.error(`[./service/useStorage]保存${CURRENT_DEPT}失败`);
                danger({
                    msg: `[./service/useStorage]保存${CURRENT_DEPT}失败`
                });
            }
        });
    };

    const setThemeMode = (mode: 'default' | 'dark') => {
        return uni.setStorage({
            key: THEME_MODE,
            data: mode,
            fail: () => {
                console.error(`[./service/useStorage]保存${THEME_MODE}失败`);
                danger({
                    msg: `[./service/useStorage]保存${THEME_MODE}失败`
                });
            }
        });
    };

    const clearThemeMode = () => {
        return uni.removeStorageSync(THEME_MODE);
    };

    const getThemeMode = (): 'default' | 'dark' | undefined => {
        return uni.getStorageSync(THEME_MODE);
    };

    const setThemePrimary = (value: string) => {
        return uni.setStorage({
            key: THEME_PRIMARY,
            data: value,
            fail: () => {
                console.error(`[./service/useStorage]保存${THEME_PRIMARY}失败`);
                danger({
                    msg: `[./service/useStorage]保存${THEME_PRIMARY}失败`
                });
            }
        });
    };

    const clearThemePrimary = () => {
        return uni.removeStorageSync(THEME_PRIMARY);
    };

    const getThemePrimary = (): string | undefined => {
        return uni.getStorageSync(THEME_PRIMARY);
    };

    return {
        getAccessTokenSync,
        setAccessToken,
        removeAccessToken,
        getUserInfoSync,
        setUserInfo,
        removeUserInfo,
        getCurrentGridSync,
        setCurrentGrid,
        getCurrentDeptSync,
        setCurrentDept,
        setThemeMode,
        clearThemeMode,
        getThemeMode,
        setThemePrimary,
        clearThemePrimary,
        getThemePrimary
    };
};
